package com.tk.exam.web.admin;

import com.tk.exam.domain.TkFile;
import com.tk.exam.exception.ApiException;
import com.tk.exam.http.Response;
import com.tk.exam.service.TkExamPaperService;
import com.tk.exam.service.TkFileService;
import com.tk.exam.utils.PageInfo;
import com.tk.exam.vo.paper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;


@RestController
@RequestMapping("tk/exam/admin/examPaper")
@Slf4j
public class ExamPaperController {
    @Autowired
    private TkExamPaperService tkExamPaperService;
    @Autowired
    private TkFileService tkFileService;

    /**
     * 试卷列表
     * @return
     */
    @PostMapping(value = "getListByPage")
    public Response searchPaperListByPageWithPage(){
        //查询分页数据
        com.github.pagehelper.PageInfo<SearchPaperListResp> pageInfoHelper = tkExamPaperService.searchPaperListByPage();
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }

    /**
     * 获取线下考试试卷列表
     * @param req
     * @return
     */
    @PostMapping(value = "getOffLinePapers")
    public Response getOffLinePapersWithPage(@RequestBody GetOffLinePaperReq req){
        if(Objects.isNull(req.getExamId()))
            throw ApiException.of("考试id不能为空");
        //查询分页数据
        com.github.pagehelper.PageInfo<TkFile> pageInfoHelper = tkFileService.getTkFileWithPage(req.getExamId());
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }

    /**
     * 查看判断题
     * @param req
     * @return
     */
    @PostMapping(value = "getStuJudgeQA")
    public Response getStuJudgeQAWithPage(@RequestBody GetStuQAReq req){
        //查询分页数据
        com.github.pagehelper.PageInfo<GetStuJudgeQAResp> pageInfoHelper = tkExamPaperService.getStuJudgeQA(req);
        return get(req,pageInfoHelper);
    }

    /**
     * 查看计算题
     * @param req
     * @return
     */
    @PostMapping(value = "getStuJSQA")
    public Response getStuJSQAWithPage(@RequestBody GetStuQAReq req){
        com.github.pagehelper.PageInfo<GetStuJSQAResp> pageInfoHelper = tkExamPaperService.getStuJSQA(req);
        return get(req,pageInfoHelper);
    }

    /**
     * 查看填空题
     * @param req
     * @return
     */
    @PostMapping(value = "getStuTKQA")
    public Response getStuTKQAWithPage(@RequestBody GetStuQAReq req){
        com.github.pagehelper.PageInfo<GetStuJSQAResp> pageInfoHelper = tkExamPaperService.getStuTKQA(req);
        return get(req,pageInfoHelper);
    }

    /**
     * 查看单选题
     * @param req
     * @return
     */
    @PostMapping(value = "getStuDXQA")
    public Response getStuDXQAWithPage(@RequestBody GetStuQAReq req){
        com.github.pagehelper.PageInfo<GetStuDXResp> pageInfoHelper = tkExamPaperService.getStuDXQA(req);
        return get(req,pageInfoHelper);
    }

    /**
     * 查看实操题
     * @param req
     * @return
     */
    @PostMapping(value = "getStuFourCQA")
    public Response getStuFourCQAWithPage(@RequestBody GetStuQAReq req){
        //todo:对状态加限制,比如考试必须结束才能查看成绩
        com.github.pagehelper.PageInfo<GetStuFourCQAResp> pageInfoHelper = tkExamPaperService.getStuFourCQA(req);
        return get(req,pageInfoHelper);
    }

    private <T> Response get(GetStuQAReq req,com.github.pagehelper.PageInfo<T> pageInfoHelper){
        //todo:泛型
        if(Objects.isNull(req.getExamId())||Objects.isNull(req.getStuId()))
            throw ApiException.of("考试id和学生id不能为空");
        if(Objects.isNull(req.getExamPaperId()))
            throw ApiException.of("试卷id不能为空");
        //查询分页数据
        PageInfo pageInfo = new PageInfo(pageInfoHelper);
        return Response.succeed(pageInfo);
    }



}
